//
//  HBQHomeHeaderView.m
//  HelloBeeQuick
//
//  Created by LiYiLin on 16/8/20.
//  Copyright © 2016年 LiYiLin. All rights reserved.
//

#import "HBQHomeHeaderView.h"
#import "LYLInfiniteDisplayView.h"
#import "HBQHomeManager.h"
#import "HBQHomeButton.h"
#import <Masonry/Masonry.h>
#import <SDWebImage/UIButton+WebCache.h>

#define KInfiniteViewH (self.frame.size.height - 60)
#define KScreenWidth   (self.frame.size.width)
#define KButtonCount 4

@interface HBQHomeHeaderView()<LYLInfiniteDisplayViewDelegate>

//轮播器
@property (nonatomic, strong) LYLInfiniteDisplayView *infiniteView;
//底部按钮
@property (nonatomic, strong) UIView * buttonView;
//轮播器数据
@property (nonatomic, strong) NSMutableArray * infiniteArray;
//按钮数据
@property (nonatomic, strong) NSMutableArray * buttonArray;
//新特惠数据
@property (nonatomic, strong) NSMutableArray * activitesArray;
//保存按钮
@property (nonatomic, strong) NSMutableArray * buttons;
//记录请求name请求参数,又来判断
@property (nonatomic, copy)   NSString * string;
//记录4个按钮是否全部设置完图片
@property (nonatomic, assign) NSInteger index;

@end


@implementation HBQHomeHeaderView

- (instancetype)initWithFrame:(CGRect)frame {

    if (self= [super initWithFrame:frame]) {
        [self setup_UI];
        [self setup_Layout];
    }
    return self;
}

/*  ============================================================  */

#pragma mark - 配置界面 -
- (void)setup_UI {
    [self addSubview:self.infiniteView];
    [self addSubview:self.buttonView];
}

/*  ============================================================  */

#pragma mark - 约束设置 -
- (void)setup_Layout {
    
    [self.infiniteView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(self);
        make.height.mas_equalTo(KInfiniteViewH);
    }];
    
    [self.buttonView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.infiniteView.mas_bottom);
        make.height.mas_equalTo(60);
        make.width.mas_equalTo(self);
    }];
}

/*  ============================================================  */

#pragma mark - 获取界面数据 -
- (void)loadHomeDataWithindex:(NSInteger)index string:(NSString *)string {
    
    NSMutableArray * array = [NSMutableArray arrayWithCapacity:8];
    [[HBQHomeManager sharedManager]loadHomeData:^(NSArray *dtoArray) {
        [dtoArray[index] enumerateObjectsUsingBlock:^(NSDictionary *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [array addObject:obj[string]];
        }];
        
        if (index == 0) {
            self.infiniteArray = array;
            self.infiniteView.imageURLStringsGroup = array;
        } else if ([string isEqualToString:@"img"] && index == 1) {
            self.buttonArray = array;
            [self setup_ButtonView];
        } else if ([string isEqualToString:@"name"] && index == 1) {
            self.buttonArray = array;
            [self setup_ButtonTitle];
        }
    }];
}

/*  ============================================================  */

#pragma mark - 设置按钮图片 -
- (void)setup_ButtonView {
    
    NSMutableArray *buttons = [[NSMutableArray alloc] init];
    for (int i = 0; i < KButtonCount; i++) {
        HBQHomeButton * button = [[HBQHomeButton alloc] init];
        button.frame = CGRectMake(i * KWIDTH / 4, 0, KWIDTH / 4, 50);

        [button sd_setImageWithURL:[NSURL URLWithString:self.buttonArray[i]] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"v2_placeholder_square"]];
        
        //先让按钮设置图片,定义一个index,记录循环的次数,当执行到第四次时,表示图全部按钮的图片都已经设置完毕.
        //然后,清空buttonArray(保存按钮图片或文本的Url数组),再开始设置文本信息.
        ++self.index;
        if (self.index == 4) {
            self.buttonArray = nil;
            self.string = @"name";
            [self loadHomeDataWithindex:1 string:self.string];
        }
        //将按钮保存到数组中
        [buttons addObject:button];
        [self.buttonView addSubview:button];
    }
    self.buttons = buttons;
}

/*  ============================================================  */

#pragma mark - 设置按钮文本 -
- (void)setup_ButtonTitle {
    //遍历保存全部按钮的数组,为按钮设置文本
    for (int i = 0; i < self.buttons.count; i++) {
        [self.buttons[i] setTitle:self.buttonArray[i] forState:UIControlStateNormal];
    }
}

/*  ============================================================  */

#pragma mark - 懒加载控件 -
- (LYLInfiniteDisplayView *)infiniteView {
    
    if (!_infiniteView) {
        _infiniteView = [LYLInfiniteDisplayView LYLInfiniteDisplayViewWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height - 50) delegate:self placeholderImage:[UIImage imageNamed:@"v2_placeholder_full_size"]];
        _infiniteView.delegate = self;
        _infiniteView.pageControlStyle = LYLInfiniteDisplayViewPageContolStyleClassic;
        _infiniteView.pageControlAliment = LYLInfiniteDisplayViewPageContolAlimentRight;
        _infiniteView.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _infiniteView.currentPageDotColor = KTHEMES;
        _infiniteView.pageDotColor = [UIColor lightGrayColor];

        [self loadHomeDataWithindex:0 string:@"img"];
    }
    return _infiniteView;
}

- (UIView *)buttonView {
    
    if (!_buttonView) {
        _buttonView = [[UIView alloc] init];
        [self loadHomeDataWithindex:1 string:@"img"];
    }
    return _buttonView;
}

/*  ============================================================  */

#pragma mark - 释放控件 -
- (void)dealloc {
    self.infiniteView = nil;
    self.buttonView = nil;
    self.buttons = nil;
}

@end





